import argparse
from key_management import generate_and_save_key, load_key_from_file
from file_utility import encrypt_file, decrypt_file

def main():
    parser = argparse.ArgumentParser(description="Enkirby - File Encryption Utility")
    parser.add_argument("-k", "--key", help="Path to the encryption key file")
    parser.add_argument("-g", "--generate", action="store_true", help="Generate a new encryption key")
    parser.add_argument("-s", "--save-key", help="Path to save the encryption key")
    parser.add_argument("-e", "--encrypt", metavar="file_path", help="Encrypt the specified file")
    parser.add_argument("-d", "--decrypt", metavar="file_path", help="Decrypt the specified file")
    args = parser.parse_args()
    
    if args.generate:
        if not args.save_key:
            parser.error("Please provide a path to save the encryption key using the -s/--save-key option.")
        generate_and_save_key(args.save_key)
        return
    if args.encrypt:
        if not args.key:
            parser.error("Please provide a path to the encryption key using the -k/--key option.")
        key = load_key_from_file(args.key)
        if key is None:
            return
        encrypt_file(args.encrypt, key)
    elif args.decrypt:
        if not args.key:
            parser.error("Please provide a path to the encryption key using the -k/--key option.")
        key = load_key_from_file(args.key)
        if key is None:
            return
        decrypt_file(args.decrypt, key)
    else:
        parser.error("Please specify either the -e/--encrypt or -d/--decrypt option with the file path.")

if __name__ == "__main__":
    main()